﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS.Data;
using ECare.BUS;

namespace ECare.Client.frms
{
    public partial class frmResourcesSupply : frmGridTemplate
    {
        public frmResourcesSupply()
        {
            InitializeComponent();

            this.Load += frmResourcesSupply_Load;
        }

        public BindingList<ResoucesInfo> source { get; set; }

         void frmResourcesSupply_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            //this.frmloading = new frmLoading();
            //frmloading.Show();
            //source = new BindingList<ResoucesInfo>(await LoadDataAsync());
            ////         var res = await LoadDataAsync();
            //this.grd.DataSource = source;
            //frmloading.Close();

            //if (!CheckPermission(12))
            //    this.btnAdd.Enabled = false;

            //if (!CheckPermission(13))
            //    this.btnEdit.Enabled = false;
        }


        //private async Task<List<ResoucesInfo>> LoadDataAsync()
        //{
        //    return await Task.Run(() => ResourcesHelper.GetAllResources());

        //}


        private void btnAdd_Click(object sender, EventArgs e)
        {
            var frm = new frmResourcesInfo();
            frm.entity = null;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<ResoucesInfo>();
            var role = entity as ResoucesInfo;
            var findresult = source.FirstOrDefault(a => a.ResourceId == role.ResourceId);
            if (findresult == null)
                this.source.Add(role);

        }
        //
        private void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex == -1)
                return;

            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as ResoucesInfo;
            var frm = new frmResourcesInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;

            var entitye = this.grd.SelectedRows[0].DataBoundItem as ResoucesInfo;
            var frm = new frmResourcesInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }
    }
}
